package com.coustemview.qianfeng.huaqiangbeistore.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.coustemview.qianfeng.huaqiangbeistore.R;
import com.coustemview.qianfeng.huaqiangbeistore.adapter.Oversear_listview_evaluate_adapter;
import com.coustemview.qianfeng.huaqiangbeistore.application.Myapplication;
import com.coustemview.qianfeng.huaqiangbeistore.config.Netconfig;
import com.coustemview.qianfeng.huaqiangbeistore.model.Oversear_evaluate_Bean;
import com.coustemview.qianfeng.huaqiangbeistore.model.Oversear_specialevaluate_handler;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mingma on 2016/9/1.
 */
public class Fragment_oversear_shopdetails3 extends Fragment {
    private ListView listview_oversear_spec;
    private Myapplication application;
    private int page=1;
    private int goodsid;
    private List<Oversear_specialevaluate_handler>list;
    private BaseAdapter adapter;
    private boolean isbottom=false;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_oversear_shopdetails3, null);
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        listview_oversear_spec.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(isbottom&&scrollState==0) {
                    loadMore(page);
                    isbottom=false;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
               isbottom=firstVisibleItem+visibleItemCount==totalItemCount;
            }
        });
    }

    private void setData() {
        listview_oversear_spec.setAdapter(adapter);

    }

    private void initData() {
        application = (Myapplication) getActivity().getApplication();
        goodsid = application.getGoodsid();
        list=new ArrayList<>();
        adapter=new Oversear_listview_evaluate_adapter(list,getActivity());
         //初始化
        loadMore(1);
    }

    private void initView(View view) {
        listview_oversear_spec= (ListView) view.findViewById(R.id.listview_oversear_spec);
    }
    private void loadMore(int position){
        String path = String.format(Netconfig.SPECIAL_CATIONDETAILS_PATH, goodsid, position);
        RequestParams params=new RequestParams(path);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
              if(result!=null) {
                  List<Oversear_specialevaluate_handler> getdata = Oversear_evaluate_Bean.getdata(result);
                   if(getdata!=null&&getdata.size()>0) {
                      list.addAll(getdata);
                       adapter.notifyDataSetChanged();
                        page++;
                   }
              }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
              Toast.makeText(getActivity(), "网络出错,请检查网络", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
